package com.niit.project.controller;

import com.niit.project.entity.User;
import com.niit.project.service.UserService;
import com.niit.project.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;

//张笑玉——用户注册
@WebServlet(name = "AddUserController", value = "/AddUserController")
public class AddUserController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受前端传入的用户信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String contact = request.getParameter("contact");
        String gender = request.getParameter("gender");
        //将用户信息存入user对象中
        User user  =new User(username,password,nickname,contact,gender);
        UserService userService = new UserServiceImpl();
        //调用用户的注册方法,进行注册
        userService.register(user);
        //重定向到用户登录界面
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
}
